package com.itkjb.leisurely.resource.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itkjb.leisurely.core.base.service.impl.BaseServiceImpl;
import com.itkjb.leisurely.core.model.SearchModel;
import com.itkjb.leisurely.resource.sys.mapper.SysUserRoleMapper;
import com.itkjb.leisurely.resource.sys.model.SysUserRoleModel;
import com.itkjb.leisurely.resource.sys.model.dto.SysRoleUserDTO;
import com.itkjb.leisurely.resource.sys.model.vo.SysUserRoelVO;
import com.itkjb.leisurely.resource.sys.model.vo.SysUserVO;
import com.itkjb.leisurely.resource.sys.service.ISysUserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户绑定角色 服务实现类
 * </p>
 *
 * @author itkjb.com
 * @since 2020-07-17
 */
@Service
public class SysUserRoleServiceImpl extends BaseServiceImpl<SysUserRoleMapper, SysUserRoleModel> implements ISysUserRoleService {


    @Override
    public IPage<SysUserVO> getUsersByRoleId(IPage<?> page, SysUserRoelVO vo) {
        return this.baseMapper.getUsersByRoleId(page,vo);
    }

    @Override
    public IPage<SysUserVO> getUsersByRoleCode(SearchModel<SysRoleUserDTO> dto) {
        return this.baseMapper.getUsersByRoleCode(dto.getPageModel(), dto.getModel());
    }

    @Override
    public IPage<SysUserVO> getUsersByRoleIdNotBind(IPage<?> page, SysUserRoelVO vo) {
        return this.baseMapper.getUsersByRoleIdNotBind(page,vo);
    }

    /**
     * 删除角色绑定的用户
     * @param vo
     * @return
     */
    @Override
    public boolean removeUserIdsByRoleId(SysUserRoelVO vo){
        QueryWrapper<SysUserRoleModel> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysUserRoleModel::getRoleId,vo.getRoleId());
        wrapper.lambda().in(SysUserRoleModel::getUserId,vo.getUserIds());
        return this.remove(wrapper);
    }

    @Override
    public List<SysUserRoleModel> getRolesByUserId(SysUserRoleModel model) {
        if(model ==null ||  StringUtils.isBlank(model.getUserId())){
            return  null;
        }
        return this.baseMapper.getRolesByUserId(model);
    }

    /**
     * wrapper 组装器，构造条件查询
     * @author itkjb.com
     * @date  2020-07-17
     * @param wrapper
     * @param model 条件
     **/
    @Override
    protected void assembleWrapper(QueryWrapper<SysUserRoleModel> wrapper, SysUserRoleModel model) {
        // （代码生成器生成）自动生成了，list查询的时候，条件组装，开发者自行删除不需要的条件
        // 角色id
        wrapper.lambda().eq(StringUtils.isNotBlank(model.getRoleId()), SysUserRoleModel::getRoleId, model.getRoleId() );
        // 用户id
        wrapper.lambda().eq(StringUtils.isNotBlank(model.getUserId()), SysUserRoleModel::getUserId, model.getUserId() );
        // 组织id 预留
        wrapper.lambda().eq(StringUtils.isNotBlank(model.getDepId()), SysUserRoleModel::getDepId, model.getDepId() );
    }

    /**
     * wrapper 组装器，组装list查询时，查询的列
     * @author itkjb.com
     * @date  2020-07-17
     * @param wrapper
     * @param model
     * @return void
     * @since 2020-07-17
     **/
    @Override
    protected void buildListSelectColumns(QueryWrapper<SysUserRoleModel> wrapper, SysUserRoleModel model){
        buildCommonSelectColumns(wrapper, model);
    }

    /**
     * 组装列表查询的列（字段）
     * @date  2020-07-17
     * @param wrapper
     * @param model
     */
    protected void buildCommonSelectColumns(QueryWrapper<SysUserRoleModel> wrapper, SysUserRoleModel model){
        // 代码生成器生成，为了简化开发时，查询list时，查询指定字段，没必要查询所有参数
        wrapper.lambda().select(
            //
            SysUserRoleModel::getId,
            // 角色id
            SysUserRoleModel::getRoleId,
            // 用户id
            SysUserRoleModel::getUserId,
            // 组织id 预留
            SysUserRoleModel::getDepId,
            SysUserRoleModel::getCreateTime
        );
    }


}
